package qq_clinet.src.com.hzk.qquser_service;

import qq_clinet.src.com.hzk.qq_common.message;
import qq_clinet.src.com.hzk.qq_common.messagetype;
import qq_clinet.src.com.hzk.qq_common.user;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;

/**
 * @auoter:jackh
 */

public class clinetconnectserverThread extends Thread{
    boolean loop=true;
    private Socket socket;
    private qq_clinet.src.com.hzk.qq_common.user user;



    @Override
    public void run() {
        while(loop){
            //开启线程之后就开始读取服务器端传送的数据
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                message message=(message) objectInputStream.readObject();

                if (message.getMessagetype().equals(messagetype.message_return_online_list)){
                    String[] str=message.getContent().split(" ");
                    System.out.println("======当前在线的用户=====");
                    for (String s:
                            str) {
                        System.out.print("用户： "+s+ "  ");
                    }
                }else if (message.getMessagetype().equals(messagetype.exit_return)){
                    loop=false;
                    System.out.println("您已经退出聊天通道");
                    manageclinetconnectserverThread.delete(user.getUserid());
                }
                else if (message.getMessagetype().equals(messagetype.si_talk_return)){//客户端显示私聊消息
                    if (!(message.getSender()==null)) {
                        System.out.println("用户 " + message.getSender() + "向您发送了一条消息： ");
                        System.out.println(message.getContent());
                    }else{
                        System.out.println("您要私聊的用户不在线！！");
                    }

                }
                else if (message.getMessagetype().equals(messagetype.talk_all_return)){
                    if (!(message.getContent()==null)){
                        System.out.println(message.getSender()+" 对大家说： "+message.getContent());
                    }
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    public clinetconnectserverThread(Socket socket,user user){
        this.socket=socket;
        this.user=user;
    }

    public Socket getSocket() {
        return socket;
    }

}
